//
// Created by Suzhen on 2017/2/24.
//

#include "IOBehavior.h"
#include "AbstractAsyncStream.h"

USING_ORION_V2

IOBehavior::~IOBehavior() {
}

IOBehavior::IOBehavior() :
        onAcceptCb_(nullptr),
        onOpenCb_(nullptr),
        onClosedCb_(nullptr),
        onDataReadCb_(nullptr) {}

void IOBehavior::setOnAcceptCallBack(OnAcceptCallBack &&cb) {
    onAcceptCb_ = std::forward<OnAcceptCallBack>(cb);
}

void IOBehavior::setOnOpenCallBack(OnOpenCallBack &&cb) {
    onOpenCb_ = std::forward<OnOpenCallBack>(cb);
}

void IOBehavior::setOnClosedCallBack(OnClosedCallBack &&cb) {
    onClosedCb_ = std::forward<OnClosedCallBack>(cb);
}

void IOBehavior::setOnDataReadCallBack(OnDataReadCallBack &&cb) {
    onDataReadCb_ = std::forward<OnDataReadCallBack>(cb);
}

void IOBehavior::notifyOnAccept(AbstractAsyncStream *s) {
    if (onAcceptCb_) {
        onAcceptCb_(s);
    }
}

void IOBehavior::notifyOnOpen(AbstractAsyncStream *s) {
    if (onOpenCb_) {
        onOpenCb_(s);
    }
}

void IOBehavior::notifyOnClosed(AbstractAsyncStream *s) {
    if (onClosedCb_) {
        onClosedCb_(s);
    }
}

void IOBehavior::notifyOnDataRead(AbstractAsyncStream *s,
                                  const RawDataType *data,
                                  size_t sz) {
    if (onDataReadCb_) {
        onDataReadCb_(s, data, sz);
    }
}
